package cn.suxiaoxiang.suwebverify;

import cn.suxiaoxiang.suwebverify.command.defaultCommand;
import cn.suxiaoxiang.suwebverify.config.MessageConfig;
import cn.suxiaoxiang.suwebverify.config.WebServer;
import cn.suxiaoxiang.suwebverify.events.PlayerJoin;
import cn.suxiaoxiang.suwebverify.events.PlayerLogoutListener;
import cn.suxiaoxiang.suwebverify.events.PluginDisableListener;
import cn.suxiaoxiang.suwebverify.events.onPlayerAction;
import cn.suxiaoxiang.suwebverify.util.AutoLoginCache;
import cn.suxiaoxiang.suwebverify.util.Metrics;
import cn.suxiaoxiang.suwebverify.util.SQLiteDownloader;
import cn.suxiaoxiang.suwebverify.util.SQLiteManager;
import org.bukkit.plugin.java.JavaPlugin;

public final class SuWebVerify extends JavaPlugin {
    //注入WebServer
    private WebServer webServer;

    // 保存插件实例
    public static SuWebVerify main;
    private static SuWebVerify instance;
    @Override
    public void onEnable() {
        //禁止删除
        //-----------------------
        int pluginId = 25405;
        Metrics metrics = new Metrics(this, pluginId);
        //-----------------------
        // 自动下载并加载 SQLite 驱动
        SQLiteDownloader.ensureDriver(this.getDataFolder());

        //打印Logo
        printSU();

        //输出启动信息
        getLogger().info("SuWebVerify 已启动！");
        main = this;
        instance = this;
        // 保存默认配置
        saveDefaultConfig();

        // 启动内嵌 Web 服务器
        webServer = new WebServer(this);
        webServer.startServer();

        // 创建数据库管理器
        SQLiteManager sqliteManager = new SQLiteManager(this);
        // 连接数据库
        SQLiteManager.connect();

        // 添加指令
        getCommand("swv").setExecutor(new defaultCommand(this));
        getCommand("swv").setTabCompleter(new defaultCommand(this));

        // 注册事件监听器
        getServer().getPluginManager().registerEvents(new onPlayerAction(this), this);
        getServer().getPluginManager().registerEvents(new PlayerLogoutListener(), this);
        getServer().getPluginManager().registerEvents(new PluginDisableListener(), this);
        getServer().getPluginManager().registerEvents(new PlayerJoin(this), this);

        //注册文件
        MessageConfig playerCommandConfig = new MessageConfig(this);
        playerCommandConfig.loadPlayerCommandConfig();

        //重载默认配置
        reloadConfig();
    }

    @Override
    public void onDisable() {
        // 关闭 Web 服务器
        webServer.stopServer();
        // 关闭数据库连接
        SQLiteManager.close();
        //输出关闭信息
        getLogger().info("SuWebVerify 已禁用！");
    }

    /**
     * 打印 Su
     */
    private void printSU() {
        String suArt =
                "\n  ███████╗██╗   ██╗\n" +
                        "  ██╔════╝██║   ██║\n" +
                        "  ███████╗██║   ██║\n" +
                        "  ╚════██║██║   ██║\n" +
                        "  ███████║╚██████╔╝\n" +
                        "  ╚══════╝ ╚═════╝\n";

        // 输出到控制台
        getLogger().info(suArt);
    }
    public static SuWebVerify getInstance() {
        return instance;
    }
}
